﻿using CMS.Controls.Messages;
using CMS.Controls.TabPageTemplate;
using CMS.Infos;
using CMS.REST;
using CMS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Windows.Forms;

namespace CMS.Controls.DisplayTabPages.SettingTabPages
{
    public class BuildingEditorAdapter : EditorAdaptor
    {
        public BuildingEditorAdapter()
        {
        }

        public override Type ElementType => typeof(Building);

        public override string ItemName => "签到地点";

        public override string API => "apis/v1/buildings";

        public override string PkField => "Name";

        public override string NameField => "Name";

        public override bool AllowIndexItem => Permission.CheckPermission(Permission.Building.Index);

        public override bool AllowCreateItem => Permission.CheckPermission(Permission.Building.Save);

        public override bool AllowUpdateItem => Permission.CheckPermission(Permission.Building.Update);

        public override bool AllowDeleteItem => Permission.CheckPermission(Permission.Building.Delete);

        private ToolTip TT_Main;
        private FormPanel FP_Main;
        private TextBox TB_Building;
        private TextBox TB_Key;
        private Button Bn_Binding;
        private Button Bn_Unbinding;


        public override void InitializeEditor(Panel editorPanel, BindingSource source)
        {
            TT_Main = new ToolTip();
            FP_Main = new FormPanel
            {
                ReadOnly = true
            };
            _SettingFormPanel(editorPanel, FP_Main);
            FP_Main.SuspendLayout();
            FormBase.ColumnDefinition[] columnD = FP_Main.CloneColumnDefinition(false, true, false);
            columnD[1].Padding.Top = columnD[1].Padding.Bottom = 15;
            TB_Building = new MyTextBox
            {
                ReadOnly = true
            };
            TB_Building.DataBindings.Add("Text", source, "Name");
            FP_Main.AddField("教学楼名", TB_Building, FormPanel.TextNoEmptyValidator);

            TB_Key = new MyTextBox
            {
                ReadOnly = true
            };
            TT_Main.SetToolTip(TB_Key, "仅用来识别教学楼是否已经绑定,可以不用理会具体的值");
            TB_Key.DataBindings.Add("Text", source, "Key");
            FP_Main.AddField("教学楼绑定", TB_Key);

            Bn_Binding = new MyButton
            {
                Text = "绑定此教学楼到此电脑"
            };
            Bn_Binding.Click += Bn_Binding_Click;
            FP_Main.AddField("绑定", Bn_Binding, null, columnD);
            TT_Main.SetToolTip(Bn_Binding, "将当前电脑的信息绑定到此教学楼上,此处的教学楼其实是值班地点,同一个电脑只能绑定一个值班地点");

            Bn_Unbinding = new MyButton
            {
                Text = "清除此教学楼绑定"
            };
            TT_Main.SetToolTip(Bn_Unbinding, "清除绑定后,助理无法使用此电脑进行值班签到");

            Bn_Unbinding.Click += Bn_Unbinding_Click;
            FP_Main.AddField("解绑", Bn_Unbinding, null, columnD);
            FP_Main.ResumeLayout();
        }

        private void Bn_Binding_Click(object sender, EventArgs e)
        {
            _SendMessage(MsgType.OK, "正在绑定中,请稍等...");
            string hash = Utils.GetComputerHash(RESTClient.GetServerTimeStamp());
            RESTResponse response = RESTClient.HttpPost("apis/v1/bind_building", new JObject() { { "name", (string)_GetPkValue(EditorSource.DataSource) }, { "key", hash } });
            if (response.IsSuccess)
            {
                _SetPropertyValue(EditorSource.DataSource, "Key", response.Data[0].ToObject<string>());
                _ForceUpdateMainList();
                _SendMessage(MsgType.OK, "绑定成功");
            }
            else
            {
                _SendMessage(MsgType.ERROR, "绑定失败");
            }
        }
        private void Bn_Unbinding_Click(object sender, EventArgs e)
        {
            RESTResponse response = RESTClient.HttpPost("apis/v1/unbind_building", new JObject() { { "name", (string)_GetPkValue(EditorSource.DataSource) } });
            if (response.IsSuccess)
            {
                _SetPropertyValue(EditorSource.DataSource, "Key", "");
                _ForceUpdateMainList();
                _SendMessage(MsgType.OK, "解绑成功");
            }
            else
            {
                _SendMessage(MsgType.ERROR, "解绑失败");
            }
        }

        public override bool UpdateDisplayMode(Panel editorPanel, DisplayMode mode)
        {
            switch (mode)
            {
                case DisplayMode.New:
                    throw new NotImplementedException();
                case DisplayMode.Edit:
                    throw new NotImplementedException();
                case DisplayMode.Preview:

                    return true;
                case DisplayMode.None:
                    FP_Main.ClearContent();
                    return true;
                default:
                    return false;
            }
        }
        public override bool ValidateForm()
        {
            return FP_Main.Validate();
        }
    }
}
